//
//  SISSAddToDoItemViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/9.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSAddToDoItemViewController.h"
#import "SISSShowView.h"
#import "SISSChooseView.h"
#import "SISSLabelTextFieldView.h"
#import "SISSTextView.h"
#import "SISSTitleView.h"
#import "SISSPickerView.h"
#import "YYRDatePickerView.h"
#import "SISSHouseProgressModel.h"
#import "SISSHouseProgressRecordInfoModel.h"
#import "SISSPassengerProgressModel.h"
#import "SISSPassengerProgressRecordInfoModel.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSUploadSuccessViewController.h"

@interface SISSAddToDoItemViewController ()<SISSChooseViewDelegate>

/// 待办业务
@property (weak, nonatomic) SISSShowView *todoItemShowView;
/// 时间
@property (weak, nonatomic) SISSChooseView *dateChooseView;
/// 地点
@property (weak, nonatomic) SISSLabelTextFieldView *addressLabelTextField;
/// 提醒角色
@property (weak, nonatomic) SISSChooseView *remindChooseView;
/// 补充事项
@property (weak, nonatomic) SISSTitleView *additionalTitleView;
@property (weak, nonatomic) SISSTextView *additionalTextView;
/// 确定
@property (weak, nonatomic) UIButton *confirmButton;

///
@property (assign, nonatomic) NSInteger role;

@end

@implementation SISSAddToDoItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"待办事项提醒";
}

- (void)setToDoItemStr:(NSString *)toDoItemStr {
    _toDoItemStr = toDoItemStr;
    self.todoItemShowView.rightStr = toDoItemStr;
}

#pragma mark - SISSChooseViewDelegate
- (void)clickChooseActionWithChooseView:(SISSChooseView *)chooseView {
    [self.view endEditing:YES];
    WeakSelf
    if (chooseView.tag == 100) { // 选择时间
        YYRDatePickerView *datePickerView = [[YYRDatePickerView alloc] initWithFrame:self.view.bounds];
        datePickerView.pickerViewMode = YYRDatePickerViewDateYearMonthDayHourMinuteMode;
        datePickerView.title = @"时间选择";
        datePickerView.confirmCompletionBlock = ^(NSString * _Nonnull date) {
            NSLog(@"%@",date);
            weakSelf.dateChooseView.rightStr = date;
        };
        [datePickerView showDateTimePickerView];
    } else if (chooseView.tag == 101) { // 选择提醒角色
        SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
        pickerView.title = @"提醒角色";
        pickerView.titleArray = @[@"只提醒买方", @"只提醒卖方", @"同时提醒买卖方"];
        pickerView.isArea = NO;
        pickerView.firstIndex = 0;
        pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
            weakSelf.role = firstIndex + 1;
            weakSelf.remindChooseView.rightStr = firstTitle;
            NSLog(@"%@",firstTitle);
        };
    }
}

#pragma mark - 按钮点击事件
- (void)confirmButtonClickAction {
    
    [self.view endEditing:YES];
    
    if (self.dateChooseView.rightStr.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择办理具体时间" andTime:2 andColor:YES];
        return;
    }
    
    if (self.addressLabelTextField.rightTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入具体地点" andTime:2 andColor:YES];
        return;
    }
    if (self.role == 0) {
        [SISSTools showSVErrorTitle:@"请选择需要提醒的角色" andTime:2 andColor:YES];
        return;
    }
    NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : self.passengerProgressRecordInfoModel.ahbpId;
    NSString *adbpId = self.passengerProgressModel ? self.passengerProgressModel.adbpId : self.houseProgressRecordInfoModel.adbpId;
    NSInteger houseType = self.houseProgressModel ? 1 : self.passengerProgressRecordInfoModel.tradeBusinessType;
    NSInteger demandType = self.passengerProgressModel ? 1 : self.houseProgressRecordInfoModel.tradeBusinessType;
     NSString *dealTime = [self.dateChooseView.rightStr stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    NSString *otherdesc = self.additionalTextView.text ? self.additionalTextView.text : @"";
    if (houseType == 2) ahbpId = self.passengerProgressRecordInfoModel.ahrId;
    if (demandType == 2) adbpId = self.houseProgressRecordInfoModel.abdId;
    NSDictionary *parameters = @{
                                 @"ahbpId": ahbpId,
                                 @"adbpId": adbpId,
                                 @"address": self.addressLabelTextField.rightTextField.text,
                                 @"deal_time": dealTime,
                                 @"demand_type": @(demandType),
                                 @"house_type": @(houseType),
                                 @"business_type": @(self.business_type),
                                 @"progress_title": self.toDoItemStr,
                                 @"node_type": self.nodeType,
                                 @"otherdesc": otherdesc,
                                 @"role": @(self.role)
                                 };
    [kNetworkManager insertAdditionalNodeWithParameters:parameters images:nil success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {
            [self pushToSuccessViewWithPopBackClassType: self.houseProgressModel ? SISSProgressPopBackClassTypeHouse : SISSProgressPopBackClassTypePassenger];
        }
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)pushToSuccessViewWithPopBackClassType:(SISSProgressPopBackClassType)popBackClassType {
    SISSUploadSuccessViewController *vc = [[SISSUploadSuccessViewController alloc] init];
    vc.popBackClassType = popBackClassType;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.containerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    SISSShowView *todoItemShowView = [[SISSShowView alloc] init];
    todoItemShowView.isShowBottomLine = YES;
    todoItemShowView.leftStr = @"待办事项";
    todoItemShowView.rightStr = self.toDoItemStr;
    [self.containerView addSubview:todoItemShowView];
    self.todoItemShowView = todoItemShowView;
    
    SISSChooseView *dateChooseView = [[SISSChooseView alloc] init];
    dateChooseView.isShowBottomLine = YES;
    dateChooseView.leftStr = @"时间";
    dateChooseView.rightPlaceStr = @"请选择办理具体时间";
    dateChooseView.delegate = self;
    dateChooseView.tag = 100;
    [self.containerView addSubview:dateChooseView];
    self.dateChooseView = dateChooseView;
    
    SISSLabelTextFieldView *addressLabelTextField = [[SISSLabelTextFieldView alloc] init];
    addressLabelTextField.leftStr = @"地点";
    addressLabelTextField.rightPlaceStr = @"请填写具体地点";
    addressLabelTextField.isShowBottomLine = YES;
    [self.containerView addSubview:addressLabelTextField];
    self.addressLabelTextField = addressLabelTextField;
    
    SISSChooseView *remindChooseView = [[SISSChooseView alloc] init];
    remindChooseView.isShowBottomLine = YES;
    remindChooseView.leftStr = @"提醒角色";
    remindChooseView.rightPlaceStr = @"请选择需要提醒的角色";
    remindChooseView.delegate = self;
    remindChooseView.tag = 101;
    [self.containerView addSubview:remindChooseView];
    self.remindChooseView = remindChooseView;
    
    SISSTitleView *additionalTitleView = [[SISSTitleView alloc] init];
    additionalTitleView.titleStr = @"补充事项";
    [self.containerView addSubview:additionalTitleView];
    self.additionalTitleView = additionalTitleView;
    
    SISSTextView *additionalTextView = [[SISSTextView alloc] init];
    additionalTextView.placeholder = @"请填写补充事项";
    [self.containerView addSubview:additionalTextView];
    self.additionalTextView = additionalTextView;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.containerView addSubview:confirmButton];
    self.confirmButton = confirmButton;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGSize buttonSize = CGSizeMake(SCREEN_WIDTH * 0.8, 54);
    [self.todoItemShowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.containerView);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.dateChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.todoItemShowView.mas_bottom);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.addressLabelTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.dateChooseView.mas_bottom);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.remindChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.addressLabelTextField.mas_bottom);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.additionalTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.remindChooseView.mas_bottom);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.additionalTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.additionalTitleView.mas_bottom);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(25 * globelMargin);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.additionalTextView.mas_bottom).offset(3 * globelMargin);
        make.centerX.equalTo(weakSelf.containerView.mas_centerX);
        make.size.mas_equalTo(buttonSize);
        make.bottom.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
    }];
    
}


@end
